﻿
using Dt261.db;
using Dt261.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Panuon.UI.Silver;
using System;


namespace Dt261.ViewModel
{
    class SettingPageViewModel:ViewModelBase
    {
       private PureToneOptions option;
        private PureToneOptionsDb db;

        private DisPlayOption displayoption;
        private DisplayOptionDb displaydb;

        private string corPsw;

        private string dblineColor;

        private int defaultDb;
        private int stepDb;
        private bool isMaintain;
        private bool isAutoJump;
        private bool isChecked125;
        private bool isChecked250;
        private bool isChecked500;
        private bool isChecked750;
        private bool isChecked1000;
        private bool isChecked1500;
        private bool isChecked2000;
        private bool isChecked3000;
        private bool isChecked4000;
        private bool isChecked6000;
        private bool isChecked8000;

        private bool isABcDifferent;
        private bool isBcLRDiffent;
        private bool isBcLinked;

        private int bcRightMark;
        private int bcMaskRightMark;

        private string rightIcon1;
        private string rightIcon2;

        public PureToneOptions Option { get => option; set { option = value; RaisePropertyChanged();} }
        public int DefaultDb { get => defaultDb; set { defaultDb = value; RaisePropertyChanged(); saveOption(); } }
        public int StepDb { get => stepDb; set { stepDb = value; RaisePropertyChanged(); saveOption(); } }


        public string RightIcon1 { get => rightIcon1; set { rightIcon1 = value; RaisePropertyChanged(); } }
        public string RightIcon2 { get => rightIcon2; set { rightIcon2 = value; RaisePropertyChanged(); } }
        public bool IsAutoJump { get => isAutoJump; set { isAutoJump = value; RaisePropertyChanged(); saveOption(); } }

    public bool IsMaintain { get => isMaintain; set { isMaintain = value; RaisePropertyChanged();  DblineColor = IsMaintain ? "gray" : "black"; saveOption(); } }

        public bool IsChecked125 { get => isChecked125; set { isChecked125 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked250 { get => isChecked250; set { isChecked250 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked500 { get => isChecked500; set { isChecked500 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked750 { get => isChecked750; set { isChecked750 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked1000 { get => isChecked1000; set { isChecked1000 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked1500 { get => isChecked1500; set { isChecked1500 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked2000 { get => isChecked2000; set { isChecked2000 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked3000 { get => isChecked3000; set { isChecked3000 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked4000 { get => isChecked4000; set { isChecked4000 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked6000 { get => isChecked6000; set { isChecked6000 = value; RaisePropertyChanged(); saveOption(); } }
        public bool IsChecked8000 { get => isChecked8000; set { isChecked8000 = value; RaisePropertyChanged(); saveOption(); } }



        public string CorPsw { get => corPsw; set { corPsw = value; RaisePropertyChanged(); } }



        public bool IsABcDifferent { get => isABcDifferent; set
            { isABcDifferent = value; RaisePropertyChanged(); displayoption.IsDifAcMasked = isABcDifferent; saveDisplayoption(); } }
        public bool IsBcLRDiffent { get => isBcLRDiffent; set
            {
                isBcLRDiffent = value; RaisePropertyChanged();
                displayoption.IsDifBcRight = isBcLRDiffent;

                if (value)
                {
                    if (BcRightMark != (int)PointDraw.POINT_TYPE_3 && BcRightMark != (int)PointDraw.POINT_TYPE_4)
                    {
                        BcRightMark = (int)PointDraw.POINT_TYPE_3;
                    }
                }
                else
                {
                    if (BcRightMark != (int)PointDraw.POINT_TYPE_13 && BcRightMark != (int)PointDraw.POINT_TYPE_10)
                    {
                        BcRightMark = (int)PointDraw.POINT_TYPE_13;
                    }
                }
                saveDisplayoption();
            } }
        public bool IsBcLinked { get => isBcLinked; set
            { isBcLinked = value; RaisePropertyChanged(); displayoption.IsLinkBc = isBcLinked; saveDisplayoption(); } }
        public int BcRightMark { get => bcRightMark; set
            { bcRightMark = value; RaisePropertyChanged();
                displayoption.BcRightType = bcRightMark;

                if(bcRightMark == 3)
                {
                    RightIcon1 = "pack://application:,,,/../Assets/Images/point_type_8.png";
                    RightIcon2 = "pack://application:,,,/../Assets/Images/point_type_6.png";
                   
                }
                else
                {
                    RightIcon1 = "pack://application:,,,/../Assets/Images/point_type_7.png";
                    RightIcon2 = "pack://application:,,,/../Assets/Images/point_type_5.png";
                }
           
                saveDisplayoption(); } }
        public int BcMaskRightMark { get => bcMaskRightMark; set
            { bcMaskRightMark = value; RaisePropertyChanged(); displayoption.BcRightMaskedType = bcMaskRightMark; saveDisplayoption(); } }

        public string DblineColor { get => dblineColor; set { dblineColor = value; RaisePropertyChanged(); } }

        public SettingPageViewModel()
        {
            db = new PureToneOptionsDb(dbconfig.DatabaseFilename);
            Option = db.GetOnePureToneOptionsByUserId(GlobalData.LoginUser.Uid);

            displaydb = new DisplayOptionDb(dbconfig.DatabaseFilename);
            displayoption = displaydb.GetOneDisPlayOptionByUserId(GlobalData.LoginUser.Uid);

            SaveCommand = new RelayCommand(saveOption);
            DownDefaultDBCommand = new RelayCommand(downDefaultDB);
            UpDefaultDBCommand = new RelayCommand(upDefaultDB);

            DownStepDBCommand = new RelayCommand(downStepDB);
            UpStepDBCommand = new RelayCommand(upStepDB);



           
            stepDb = Option.StepValue;

            isAutoJump =  Option.IsAutoJump;

           isChecked125 = Option.IsChecked125;
           isChecked250  =  Option.IsChecked250;
           isChecked500  =  Option.IsChecked500;
           isChecked750  =  Option.IsChecked750;
            isChecked1000 = true;// Option.IsChecked1000 ;
           isChecked1500 =  Option.IsChecked1500;
           isChecked2000 =  Option.IsChecked2000;
           isChecked3000 =  Option.IsChecked3000;
           isChecked4000 =  Option.IsChecked4000;
           isChecked6000 =  Option.IsChecked6000;
           isChecked8000 =  Option.IsChecked8000;
           isMaintain = Option.IsMaintain;

            DblineColor = IsMaintain ? "gray" : "black";
            defaultDb = IsMaintain ? 30: Option.DefaultValue;


            isABcDifferent = displayoption.IsDifAcMasked;
            isBcLRDiffent = displayoption.IsDifBcRight;
            isBcLinked = displayoption.IsLinkBc;

            bcRightMark = displayoption.BcRightType;
            bcMaskRightMark = displayoption.BcRightMaskedType;

            rightIcon1 = "pack://application:,,,/../Assets/Images/point_type_7.png";
            rightIcon2 = "pack://application:,,,/../Assets/Images/point_type_5.png";
        }

        public RelayCommand SaveCommand { get; set; }
        public RelayCommand DownDefaultDBCommand { get; set; }
        public RelayCommand UpDefaultDBCommand { get; set; }
        public RelayCommand DownStepDBCommand { get; set; }
        public RelayCommand UpStepDBCommand { get; set; }
      

        public void downStepDB()
        {
            StepDb -= 1;
            if (StepDb < 1)
            {
                StepDb = 1;
            }
            else if(StepDb > 5)
            {
                StepDb = 5;
            }
            Option.StepValue = StepDb;
            db.UpdatePureToneOptions(Option);

        }
        public void upStepDB()
        {
            StepDb += 1;
            if (StepDb < 1)
            {
                StepDb = 1;
            }
            else if (StepDb > 5)
            {
                StepDb = 5;
            }
            Option.StepValue = StepDb;
            db.UpdatePureToneOptions(Option);
        }
        public void downDefaultDB()
        {
            DefaultDb -= 5;
            if(DefaultDb < 0)
            {
                DefaultDb = 0;
            }
            else if(DefaultDb> 35)
            {
                DefaultDb = 35;
            }

            Option.DefaultValue = DefaultDb;
            db.UpdatePureToneOptions(Option);

        }
        public void upDefaultDB()
        {
            DefaultDb += 5;
            if (DefaultDb < 0)
            {
                DefaultDb = 0;
            }
            else if (DefaultDb > 35)
            {
                DefaultDb = 35;
            }

            Option.DefaultValue = DefaultDb;
            db.UpdatePureToneOptions(Option);
        }
        public void saveOption()
        {
           // PureToneOptions Option = db.GetOnePureToneOptionsByUserId(GlobalData.LoginUser.Uid);
            Option.IsMaintain = IsMaintain;
            Option.DefaultValue = DefaultDb;
             Option.IsAutoJump = IsAutoJump;
            Option.IsChecked125 = IsChecked125;
            Option.IsChecked250 = IsChecked250;
                Option.IsChecked500 = IsChecked500;
                Option.IsChecked750 = IsChecked750;
                Option.IsChecked1000 = IsChecked1000;
                Option.IsChecked1500 = IsChecked1500;
                Option.IsChecked2000 = IsChecked2000;
                Option.IsChecked3000 = IsChecked3000;
                Option.IsChecked4000 = IsChecked4000;
                Option.IsChecked6000 = IsChecked6000;
                Option.IsChecked8000 = IsChecked8000;
           

            Option.StepValue = StepDb;
            db.UpdatePureToneOptions(Option);
           // Notice.Show("听力设置已保存生效.", "提示", 3, MessageBoxIcon.Success);
        }

        public void saveDisplayoption()
        {
            //DisPlayOption displayoption = displaydb.GetOneDisPlayOptionByUserId(GlobalData.LoginUser.Uid);

   

            displaydb.UpdateDisPlayOption(displayoption);

            displayoption = displaydb.GetOneDisPlayOptionByUserId(GlobalData.LoginUser.Uid);
        }

    }
}
